﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;

namespace ScoreMS.Views
{
    /// <summary>
    /// ScoreMultiAdd.xaml 的交互逻辑
    /// </summary>
    public partial class ScoreMultiAdd : Page
    {
        public ScoreMultiAdd()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ScoreMultiAdd_Loaded);
        }

        void ScoreMultiAdd_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbxCourse.ItemsSource = BLL.CourseBS.GetCourseList(App.teacher_id);
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (cbxCourse.SelectedIndex == -1 || txtStudentCount.Text.Trim() == "" || lstEx.SelectedIndex == -1)
                {
                    MessageBox.Show("请填写完整信息！", "提示");
                    return;
                }
                int studentCount;
                if (!int.TryParse(txtStudentCount.Text.Trim(), out studentCount))
                {
                    MessageBox.Show("学生数为阿拉伯数字，如100！", "警告");
                    return;
                }
                var course_id = (cbxCourse.SelectedItem as Model.Course).course_id;
                int ex_id = (lstEx.SelectedItem as Model.Examination_papar).ex_id;

                OpenFileDialog open = new OpenFileDialog();
                open.Title = "选择导入成绩的xls文件";
                open.Filter = "Excel文件（.xls）|*.xls";
                if ((bool)open.ShowDialog().GetValueOrDefault())
                {
                    FileStream file = new FileStream(open.FileName, FileMode.Open, FileAccess.Read);
                    if (BLL.IO.InputScore(file, course_id, ex_id, studentCount))
                    {
                        MessageBox.Show("添加成功！", "提示");
                    }
                    else
                    {
                        MessageBox.Show("添加失败！","警告");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败！" + ex.ToString(), "警告");
            }
        }

        /// <summary>
        /// 当选择课程改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxCourse_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxCourse.SelectedIndex == -1) return;
            var course_id = (cbxCourse.SelectedItem as Model.Course).course_id;
            this.lstEx.ItemsSource = BLL.Examination_paparBS.GetExamination_paparList(course_id);
        }
    }
}
